let rootUrl = "https://studyapi.qianbijiaoyu.cn/api/";//具体接口域名根据你的实际情况填写
function getData(url, data, cb) {
  
  wx.request({
    url: rootUrl + url,
    data: data,
    method: 'get',
    header: {
      "Content-Type": "json",   //get请求时候
    },
    success: function (res) {
      wx.hideLoading();
      if (res.code==101){//未授权登录
        wx.showModal({
          title: '提示',
          content: '登录失效，请重新登录',
          showCancel:false,
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/author/author',
              })
            }
          }
        })

      } 
      return typeof cb == "function" && cb(res.data)
    },
    fail: function () {
      wx.hideLoading();
      return typeof cb == "function" && cb(false)
    }
  })
}



function postData(url, data, cb) {
  
  wx.request({
    url: rootUrl + url,
    data: data,
    method: 'post',
    header: {
      "Content-Type": "application/x-www-form-urlencoded" //POST请求的时候这样写
    },
    success: function (res) {
      wx.hideLoading();
      if (res.code == 101) {//未授权登录
        wx.showModal({
          title: '提示',
          content: '登录失效，请重新登录',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/author/author',
              })
            }
          }
        })

      } 
      return typeof cb == "function" && cb(res.data)
    },
    fail: function () {
      wx.hideLoading();
      return typeof cb == "function" && cb(false)
    }
  })
}





module.exports = {
  getData: getData, //暴露一个方法
  postData:postData
}